package com.banciyuan.bcywebview.biz.detail.complex.holder;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.detail.AdHelper;
import com.banciyuan.bcywebview.biz.event.EventDetailActivity;
import com.banciyuan.bcywebview.biz.groupdetail.NewTeamDetailActivity;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.baidussp.SimpleImplOnAdlistener;
import com.banciyuan.bcywebview.utils.baidussp.SspHelper;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.sys.SystemUtils;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.Complex;
import de.greenrobot.daoexample.model.TagDetail;

/**
 * Created by squall on 2016/8/10.
 */
public class WorkAcgHolder extends RecyclerView.ViewHolder {
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private View viewAddsLine;
    private LinearLayout recommendDetailContainer;
    private View recommendDetailView;
    private View recommendMore;
    private TextView recommendTv;

    public WorkAcgHolder(View itemView, Context mContext, int timelineType) {
        super(itemView);
        this.mContext = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
        viewAddsLine = itemView.findViewById(R.id.view_ads_line);
        createAdView((ImageView) itemView.findViewById(R.id.view_ads), mContext, timelineType);
        recommendDetailView = itemView.findViewById(R.id.view_recommend_content);
        recommendDetailContainer = (LinearLayout) itemView.findViewById(R.id.recommend_content_container);
        recommendMore = itemView.findViewById(R.id.more_line);
        recommendTv = (TextView) itemView.findViewById(R.id.recommend_rela_tv);
    }

    private void createAdView(final ImageView itemView, final Context mContext, int timelineType) {
        int height = (int) (CommonUtils.getWidthPixels((Activity) mContext) / MyConstants.advScale);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, height);
        itemView.setLayoutParams(layoutParams);
        int width = SystemUtils.getWidth(mContext);

        final SspHelper sspHelper = new SspHelper(mContext, width, height);
        if (timelineType == TimelineFactory.TIMELINE_TYPE_DAILY) {
            sspHelper.getAD(MyConstants.ADS_ID_DAILY, new SspHelper.onAdverListener() {
                @Override
                public void onSucc(String responce) {
                    AdHelper adHelper = new AdHelper(mContext, MyConstants.ADS_ID_DAILY);
                    adHelper.parseHtml(itemView, viewAddsLine, responce);
                    sspHelper.getViewCountImpress(MyConstants.ADS_ID_DAILY, new SimpleImplOnAdlistener(), 130);
                }

                @Override
                public void onfail(String error) {
                    viewAddsLine.setVisibility(View.GONE);
                    itemView.setVisibility(View.GONE);
                }
            });

        } else if (timelineType == TimelineFactory.TIMELINE_TYPE_COSER) {
            sspHelper.getAD(MyConstants.ADS_ID_COSER, new SspHelper.onAdverListener() {
                @Override
                public void onSucc(String responce) {
                    AdHelper adHelper = new AdHelper(mContext, MyConstants.ADS_ID_COSER);
                    adHelper.parseHtml(itemView, viewAddsLine, responce);
                    sspHelper.getViewCountImpress(MyConstants.ADS_ID_COSER, new SimpleImplOnAdlistener(), 130);

                }

                @Override
                public void onfail(String error) {
                    viewAddsLine.setVisibility(View.GONE);
                    itemView.setVisibility(View.GONE);
                }
            });
        } else if (timelineType == TimelineFactory.TIMELINE_TYPE_ILLUST) {
            sspHelper.getAD(MyConstants.ADS_ID_DRAWER, new SspHelper.onAdverListener() {
                @Override
                public void onSucc(String responce) {
                    AdHelper adHelper = new AdHelper(mContext, MyConstants.ADS_ID_DRAWER);
                    adHelper.parseHtml(itemView, viewAddsLine, responce);
                    sspHelper.getViewCountImpress(MyConstants.ADS_ID_DRAWER, new SimpleImplOnAdlistener(), 130);

                }

                @Override
                public void onfail(String error) {
                    viewAddsLine.setVisibility(View.GONE);
                    itemView.setVisibility(View.GONE);
                }
            });
        } else if (timelineType == TimelineFactory.TIMELINE_TYPE_WRITER) {
            sspHelper.getAD(MyConstants.ADS_ID_NOVEL, new SspHelper.onAdverListener() {
                @Override
                public void onSucc(String responce) {
                    AdHelper adHelper = new AdHelper(mContext, MyConstants.ADS_ID_NOVEL);
                    adHelper.parseHtml(itemView, viewAddsLine, responce);
                    sspHelper.getViewCountImpress(MyConstants.ADS_ID_DRAWER, new SimpleImplOnAdlistener(), 130);

                }

                @Override
                public void onfail(String error) {
                    viewAddsLine.setVisibility(View.GONE);
                    itemView.setVisibility(View.GONE);
                }
            });
        } else {
            viewAddsLine.setVisibility(View.GONE);
            itemView.setVisibility(View.GONE);
        }
    }

    public void setData(final Complex mItem, int timelineType) {
        setRecommendDetail(mItem, timelineType);
    }

    private void setRecommendDetail(final Complex mItem, final int timelineType) {
        recommendDetailView.setVisibility(View.GONE);
        if (timelineType == TimelineFactory.TIMELINE_TYPE_GROUP) {
            recommendMore.setVisibility(View.VISIBLE);
            recommendTv.setText(mContext.getString(R.string.other_answer));
            setGroupRelaDetail(mItem.getRecommend_rela());
            recommendMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, NewTeamDetailActivity.class, mItem.getGid());
                }
            });
        } else {
            recommendMore.setVisibility(View.GONE);
            createCommonRelaDetail(timelineType, mItem.getRecommend_rela(), mItem.getcRecommendCircle());
        }
    }

    private void setGroupRelaDetail(List<Complex> recommendRela) {
        if (recommendRela.isEmpty()) {
            return;
        }
        recommendDetailView.setVisibility(View.VISIBLE);
        recommendDetailContainer.removeAllViews();
        for (int i = 0; i < recommendRela.size(); i++) {
            final Complex complex = recommendRela.get(i);
            View viewDetail = mLayoutInflater.inflate(R.layout.detail_recommend_group_item, recommendDetailContainer, false);
            ImageView ivDailyCover = (ImageView) viewDetail.findViewById(R.id.iv_daily_cover);
            TextView tvDailyTitle = (TextView) viewDetail.findViewById(R.id.tv_daily_title);
            View longDailyShadow = viewDetail.findViewById(R.id.view_longdaily_shadow);
            TextView tvLongDailyTitle = (TextView) viewDetail.findViewById(R.id.tv_longdaily_title);
            View commonDailyShadow = viewDetail.findViewById(R.id.view_commondaily_shadow);
            createGroupDetail(complex, viewDetail, ivDailyCover, tvDailyTitle, longDailyShadow, commonDailyShadow, tvLongDailyTitle);
        }
    }

    private void setDailyRelaDetail(List<Complex> recommendRela) {
        for (int i = 0; i < recommendRela.size(); i++) {
            final Complex complex = recommendRela.get(i);
            View viewDetail = mLayoutInflater.inflate(R.layout.detail_recommend_daily_item, recommendDetailContainer, false);
            ImageView ivDailyCover = (ImageView) viewDetail.findViewById(R.id.iv_daily_cover);
            TextView tvDailyTitle = (TextView) viewDetail.findViewById(R.id.tv_daily_title);
            View longDailyShadow = viewDetail.findViewById(R.id.view_longdaily_shadow);
            TextView tvLongDailyTitle = (TextView) viewDetail.findViewById(R.id.tv_longdaily_title);
            View commonDailyShadow = viewDetail.findViewById(R.id.view_commondaily_shadow);
            View viewArticleMark = viewDetail.findViewById(R.id.tv_larticle_item_type);
            View viewIcon = viewDetail.findViewById(R.id.video_icon);
            if ("daily".equals(complex.getType())) {
                createCommonDailyDetail(complex, viewDetail, ivDailyCover, tvDailyTitle, longDailyShadow, commonDailyShadow, tvLongDailyTitle, viewArticleMark, viewIcon);
            } else if ("larticle".equals(complex.getType())) {
                createLongDailyDetail(complex, viewDetail, ivDailyCover, tvDailyTitle, longDailyShadow, commonDailyShadow, tvLongDailyTitle, viewArticleMark, viewIcon);
            } else if ("svideo".equals(complex.getType())) {
                createVideoDailyDetail(complex, viewDetail, ivDailyCover, tvDailyTitle, longDailyShadow, commonDailyShadow, tvLongDailyTitle, viewArticleMark, viewIcon);
            }
        }
    }

    private void createVideoDailyDetail(final Complex complex, View viewDetail, ImageView ivDailyCover, TextView tvDailyTitle, View longDailyShadow, View commonDailyShadow, TextView tvLongDailyTitle, View viewArticleMark, View videoIcon) {
        commonDailyShadow.setVisibility(View.GONE);
        longDailyShadow.setVisibility(View.GONE);
        ivDailyCover.setVisibility(View.VISIBLE);
        tvDailyTitle.setVisibility(View.GONE);
        videoIcon.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(complex.getCover())) {
            ImageLoader.getInstance().displayImage(complex.getCover(), ivDailyCover, ImageOptions.options);
        } else {
            ivDailyCover.setImageResource(R.drawable.larticle_cover);
        }
        viewDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailDataHelper.gotoDailyDetail(mContext, complex.getUd_id(), false);
            }
        });
        recommendDetailContainer.addView(viewDetail);
    }

    private void setNovelRecommendDetail(List<Complex> recommendRela) {
        for (int i = 0; i < recommendRela.size(); i++) {
            final Complex novel = recommendRela.get(i);
            View viewDetail = mLayoutInflater.inflate(R.layout.detail_recommend_write_item, recommendDetailContainer, false);
            TextView tvWriteTitle = (TextView) viewDetail.findViewById(R.id.tv_write_title);
            TextView tvWorkTag = (TextView) viewDetail.findViewById(R.id.tv_work_tag);
            ImageView ivWriteCover = (ImageView) viewDetail.findViewById(R.id.iv_write_cover);
            tvWriteTitle.setText(Html.fromHtml(novel.getTitle()));
            StringBuilder sb = new StringBuilder();
            if (!TextUtils.isEmpty(novel.getWork())) {
                sb.append(String.format(mContext.getString(R.string.detail_novel_rela_work_format), novel.getWork()));
            }
            if (null != novel.getPost_tags())
                for (int j = 0; j < novel.getPost_tags().size(); j++) {
                    sb.append(String.format(mContext.getString(R.string.detail_novel_rela_work_format), novel.getPost_tags().get(j).getTag_name()));
                }
            if (sb.length() > 3)
                tvWorkTag.setText(sb.toString().substring(0, sb.length() - 3));
            ImageLoader.getInstance().displayImage(recommendRela.get(i).getCover(), ivWriteCover, ImageOptions.options);
            viewDetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DetailDataHelper.gotoWriterDetail(mContext, novel.getRp_id(), novel.getWp_id(), false);
                }
            });
            recommendDetailContainer.addView(viewDetail);
        }
    }

    private void createGroupDetail(final Complex complex, View viewDetail, ImageView ivDailyCover, TextView tvDailyTitle, View longDailyShadow, View commonDailyShadow, TextView tvLongDailyTitle) {
        ivDailyCover.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(complex.getImg_src())) {
            commonDailyShadow.setVisibility(View.GONE);
            longDailyShadow.setVisibility(View.VISIBLE);
            tvLongDailyTitle.setVisibility(View.VISIBLE);
            tvDailyTitle.setVisibility(View.GONE);
            ImageLoader.getInstance().displayImage(complex.getImg_src(), ivDailyCover, ImageOptions.options);
            if (!TextUtils.isEmpty(complex.getPlain())) {
                tvLongDailyTitle.setText(Html.fromHtml(complex.getPlain()));
            } else {
                tvLongDailyTitle.setVisibility(View.GONE);
            }
        } else {
            tvDailyTitle.setVisibility(View.VISIBLE);
            tvLongDailyTitle.setVisibility(View.GONE);
            longDailyShadow.setVisibility(View.GONE);
            commonDailyShadow.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(complex.getPlain())) {
                tvDailyTitle.setText(Html.fromHtml(complex.getPlain()));
            } else {
                tvDailyTitle.setVisibility(View.GONE);
            }
        }
        viewDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailDataHelper.gotoGroupDetail(mContext, complex.getGid(), complex.getPost_id(), false);
            }
        });
        recommendDetailContainer.addView(viewDetail);
    }

    private void createLongDailyDetail(final Complex complex, View viewDetail, ImageView ivDailyCover,
                                       TextView tvDailyTitle, View longDailyShadow, View commonDailyShadow,
                                       TextView tvLongDailyTitle, View viewArticleMark, View videoIcon) {
        viewArticleMark.setVisibility(View.VISIBLE);
        commonDailyShadow.setVisibility(View.GONE);
        longDailyShadow.setVisibility(View.VISIBLE);
        ivDailyCover.setVisibility(View.VISIBLE);
        tvDailyTitle.setVisibility(View.GONE);
        videoIcon.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(complex.getTitle())) {
            tvLongDailyTitle.setText(Html.fromHtml(complex.getTitle()));
            tvLongDailyTitle.setVisibility(View.VISIBLE);
        } else {
            tvLongDailyTitle.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(complex.getCover())) {
            ImageLoader.getInstance().displayImage(complex.getCover(), ivDailyCover, ImageOptions.options);
        } else {
            ivDailyCover.setImageResource(R.drawable.larticle_cover);
        }
        viewDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailDataHelper.gotoDailyDetail(mContext, complex.getUd_id(), false);
            }
        });
        recommendDetailContainer.addView(viewDetail);
    }

    private void createCommonDailyDetail(final Complex complex, View viewDetail, ImageView ivDailyCover,
                                         TextView tvDailyTitle, View longDailyShadow, View commonDailyShadow,
                                         TextView tvLongDailyTitle, View viewArticleMark, View videoIcon) {
        viewArticleMark.setVisibility(View.GONE);
        longDailyShadow.setVisibility(View.GONE);
        tvLongDailyTitle.setVisibility(View.GONE);
        videoIcon.setVisibility(View.GONE);
        if (TextUtils.isEmpty(complex.getImg_src())) {
            ivDailyCover.setVisibility(View.GONE);
            tvDailyTitle.setVisibility(View.VISIBLE);
            commonDailyShadow.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(complex.getPlain()))
                tvDailyTitle.setText(Html.fromHtml(complex.getPlain()));
        } else {
            ivDailyCover.setVisibility(View.VISIBLE);
            tvDailyTitle.setVisibility(View.GONE);
            commonDailyShadow.setVisibility(View.GONE);
            ImageLoader.getInstance().displayImage(complex.getImg_src(), ivDailyCover, ImageOptions.options);
        }
        viewDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailDataHelper.gotoDailyDetail(mContext, complex.getUd_id(), false);
            }
        });
        recommendDetailContainer.addView(viewDetail);
    }

    private void createCommonRelaDetail(final int timelineType, List<Complex> recommendRela, List<TagDetail> recommendCircle) {
        if (recommendCircle.isEmpty() && recommendRela.isEmpty()) {
            return;
        }
        recommendDetailView.setVisibility(View.VISIBLE);
        recommendDetailContainer.removeAllViews();
        if (recommendRela.size() >= 6) {
            if (!recommendCircle.isEmpty()) {
                setRecommendCircleItem(recommendCircle.get(0));
            }
            if (timelineType == TimelineFactory.TIMELINE_TYPE_COSER || timelineType == TimelineFactory.TIMELINE_TYPE_ILLUST) {
                setRecommendCommonItem(timelineType, recommendRela);
            } else if (timelineType == TimelineFactory.TIMELINE_TYPE_WRITER) {
                setNovelRecommendDetail(recommendRela);
            } else if (timelineType == TimelineFactory.TIMELINE_TYPE_DAILY) {
                setDailyRelaDetail(recommendRela);
            }
            for (int i = 1; i < recommendCircle.size(); i++) {
                setRecommendCircleItem(recommendCircle.get(i));
            }
        } else {
            for (int i = 0; i < recommendCircle.size(); i++) {
                setRecommendCircleItem(recommendCircle.get(i));
            }
        }


    }

    private void setRecommendCommonItem(final int timelineType, List<Complex> recommendRela) {
        for (int i = 0; i < recommendRela.size(); i++) {
            final Complex complex = recommendRela.get(i);
            ImageView ivCover = (ImageView) mLayoutInflater.inflate(R.layout.detail_recommend_pic_item, recommendDetailContainer, false);
            ImageLoader.getInstance().displayImage(complex.getImg_src(), ivCover, ImageOptions.options);
            ivCover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (timelineType == TimelineFactory.TIMELINE_TYPE_COSER) {
                        DetailDataHelper.gotoCosDetail(mContext, complex.getRp_id(), complex.getCp_id(), false);
                    } else if (timelineType == TimelineFactory.TIMELINE_TYPE_ILLUST) {
                        DetailDataHelper.gotoDrawerDetail(mContext, complex.getRp_id(), complex.getDp_id(), false);
                    } else if (timelineType == TimelineFactory.TIMELINE_TYPE_WRITER) {
                        DetailDataHelper.gotoDrawerDetail(mContext, complex.getRp_id(), complex.getWp_id(), false);
                    }
                }
            });
            recommendDetailContainer.addView(ivCover);
        }
    }

    private void setRecommendCircleItem(final TagDetail tagDetail) {
        View viewCircleItem = mLayoutInflater.inflate(R.layout.detail_recommend_circle, recommendDetailContainer, false);
        ImageView ivCircleCover = (ImageView) viewCircleItem.findViewById(R.id.iv_circle_cover);
        TextView tvCircleItemType = (TextView) viewCircleItem.findViewById(R.id.tv_item_type);
        TextView tvCircleTitle = (TextView) viewCircleItem.findViewById(R.id.tv_item_title);
        TextView tvCircleWorkCounts = (TextView) viewCircleItem.findViewById(R.id.tv_circle_workcounts);
        View viewCircleShadow = viewCircleItem.findViewById(R.id.view_circle_shadow);
        ImageLoader.getInstance().displayImage(tagDetail.getCover(), ivCircleCover, ImageOptions.options);

        if ("work".equals(tagDetail.getType())) {
            setWorkPostCounts(tagDetail, tvCircleWorkCounts);
            tvCircleItemType.setText(mContext.getString(R.string.circle));
            tvCircleTitle.setText(tagDetail.getName());
            viewCircleItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, CircleSmoothActivity.class, tagDetail.getName(), tagDetail.getWid());
                }
            });
            viewCircleShadow.setVisibility(View.VISIBLE);
            viewCircleShadow.setBackgroundResource(R.drawable.shape_bg50_70_corner);
        } else if ("tag".equals(tagDetail.getType())) {
            setWorkPostCounts(tagDetail, tvCircleWorkCounts);
            tvCircleItemType.setText(mContext.getString(R.string.circle));
            tvCircleTitle.setText(tagDetail.getTag_name());
            viewCircleItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, CircleTagSmoothActivity.class, tagDetail.getTag_name(), tagDetail.getTag_id());
                }
            });
            viewCircleShadow.setVisibility(View.VISIBLE);
            viewCircleShadow.setBackgroundResource(R.drawable.shape_bg50_70_corner);
        } else if ("event".equals(tagDetail.getType())) {
            tvCircleWorkCounts.setVisibility(View.GONE);
            tvCircleItemType.setText(mContext.getString(R.string.event));
            tvCircleTitle.setText(tagDetail.getTag_name());
            viewCircleItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, EventDetailActivity.class, tagDetail.getEvent_id());
                }
            });
            viewCircleShadow.setVisibility(View.GONE);
        }
        recommendDetailContainer.addView(viewCircleItem);
    }

    private void setWorkPostCounts(TagDetail tagDetail, TextView tvCircleWorkCounts) {
        tvCircleWorkCounts.setVisibility(View.VISIBLE);
        if (TextUtils.isEmpty(tagDetail.getPost_count())) {
            tvCircleWorkCounts.setText(String.format(mContext.getString(R.string.work_counts_format), "0"));
        } else {
            tvCircleWorkCounts.setText(String.format(mContext.getString(R.string.work_counts_format), tagDetail.getPost_count()));
        }
    }

}